package com.libinshen.wxpay.common.util.common;


import com.libinshen.wxpay.common.constant.DatePattern;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author lbs
 */
public class DateUtils {


    /***
     *验证用户输入的出生年月是否正确
     */
    public static boolean validBirthday(String birthday) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            simpleDateFormat.parse(birthday);
        } catch (ParseException e) {
            return false;
        }
        return true;
    }

    /**
     * 根据固定的格式，将字符串转化为Date
     *
     * @param str
     * @param ftm
     * @return
     */
    public static Date parseDate(String str, String ftm) {
        if (str == null) {
            return null;
        }
        try {
            return new SimpleDateFormat(ftm).parse(str);
        } catch (ParseException e) {
            return null;
        }

    }

    /**
     * 获取过去的天数
     *
     * @param date
     * @return
     */
    public static long pastDays(Date date) {
        long time = date.getTime();
        long t = System.currentTimeMillis() - time;
        return t / (24 * 60 * 60 * 1000);
    }

    /**
     * 获取过去的小时
     *
     * @param date
     * @return
     */
    public static long pastHour(Date date) {
        long t = System.currentTimeMillis() - date.getTime();
        return t / (60 * 60 * 1000);
    }

    /**
     * 获取过去的分钟
     *
     * @param date
     * @return
     */
    public static long pastMinutes(Date date) {
        long t = System.currentTimeMillis() - date.getTime();
        return t / (60 * 1000);
    }

    /**
     * 转换为时间（天,时:分:秒.毫秒）
     *
     * @param timeMillis
     * @return
     */
    public static String formatDateTime(long timeMillis) {
        long day = timeMillis / (24 * 60 * 60 * 1000);
        long hour = (timeMillis / (60 * 60 * 1000) - day * 24);
        long min = ((timeMillis / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (timeMillis / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        long sss = (timeMillis - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - s * 1000);
        return (day > 0 ? day + "," : "") + hour + ":" + min + ":" + s + "." + sss;
    }


    public static String formatDateTime(Date date) {
        return null == date ? null : DatePattern.NORM_DATETIME_FORMAT.format(date);
    }

    /**
     * 获取两个日期之间的天数，Date类型
     *
     * @param before
     * @param after
     * @return
     */
    public static double getDistanceOfTwoDate(Date before, Date after) {
        long beforeTime = before.getTime();
        long afterTime = after.getTime();
        return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
    }

    /**
     * 获取两个日期之间的天数，字符串格式
     *
     * @param before
     * @param after
     * @param fmt    : 字符串的日期格式
     * @return
     */
    public static double getDistanceOfTwoDate(String before, String after, String fmt) {
        Date beforeD = parseDate(before, fmt);
        Date afterD = parseDate(after, fmt);

        return getDistanceOfTwoDate(beforeD, afterD);
    }

    /**
     * @param before
     * @param after
     * @return
     * @Description: 获取两个日期之间的小时数
     */
    public static double getDistHoursOfTwoDate(Date before, Date after) {
        long beforeTime = before.getTime();
        long afterTime = after.getTime();
        return (afterTime - beforeTime) / (1000 * 60 * 60);
    }

    /**
     * @param start
     * @param end
     * @return
     * @Description: 获取两个时间相差的分钟数
     */
    public static Integer diffMinute(Date start, Date end) {
        return (int) Math.ceil((double) (end.getTime() - start.getTime()) / (1000 * 60));
    }

    /**
     * 通过毫秒时间戳获取小时数和分钟数
     *
     * @param time
     * @return
     */
    public static String getHourAndMinute(long time) {
        int minute = (int) Math.ceil((double) (time) / (1000 * 60));
        int hours = (int) Math.floor((double) minute / 60);
        minute = minute % 60;
        StringBuilder sb = new StringBuilder();
        if (hours > 0) {
            sb.append(hours).append("小时");
        }
        if (minute > 0) {
            sb.append(minute).append("分");
        }
        return sb.toString();
    }

    public static Integer getAgeByBirthday(String birthday) {
        Date date = new Date();
        String dateStr = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date);
        String datea = dateStr.substring(0, 4);
        String ageStr = birthday.substring(0, 4);
        int age = Integer.parseInt(datea) - Integer.parseInt(ageStr);
        return age;
    }

    public static String getYear() {
        Calendar cal = Calendar.getInstance();
        return String.valueOf(cal.get(Calendar.YEAR));
    }

    public static String getMonth() {
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        String str = df.format(date);
        String mm = str.substring(4, 6);
        return mm;
    }

    /**
     * 获取  某一年某一月中的 第几天
     *
     * @return
     */
    public static String getDd() {
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        String str = df.format(date);
        String dd = str.substring(6, 8);
        return dd;
    }

    /**
     * 获取年月日时分秒
     *
     * @return
     */
    public static String getDateMonth() {
        long l = System.currentTimeMillis();
        //new日期对象
        Date date = new Date(l);
        //转换提日期输出格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        return format;
    }

    public static String getCommonDate() {
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String dateTime = df.format(date);
        return dateTime;
    }

    public static void main(String[] args) {

    }
}
